<?php
/*
Plugin Name: BPDEV-GROUPS-EXTRA
Plugin URI: http://bp-dev.org/projects/#groups-extra
Description: Add extra group fields
Author: Nicola Greco
Version: 0.3
Author URI: http://nicolagreco.com/

Copyright (c) 2009-2011, Nicola Greco (mail: notsecurity@gmail.com | website: http://nicolagreco.com).

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

define( 'BPDEV_GROUPS_EXTRA_NAME', 'BPDEV-Groups-Extra' );
define( 'BPDEV_GROUPS_EXTRA_VERSION', 0.3 );
define( 'BPDEV_GROUPS_EXTRA_DB_VERSION', 0.2 );

require_once( 'bp-core.php' );
require_once( 'bpdev-core.php' );

if ( !get_option( 'bpdev-group_extra-status' ) ) add_option( 'bpdev-group_extra-status' , 'off' );

add_action( 'bpdev_admin_plugins_screen' , 'bpdev_group_extra_admin_menu_screen', 3 );
add_action( 'bpdev_admin_plugins_save' , 'bpdev_group_extra_admin_menu_save', 3 );


if ( 'on' == get_option( 'bpdev-group_extra-status' ) ) {

	add_action( 'wp', 'bpdev_groups_extra_fields_setup_globals', 1 );
	add_action( 'admin_menu', 'bpdev_groups_extra_fields_setup_globals', 1 );

	add_action( 'wp', 'bpdev_groups_extra_setup_globals', 1 );
	add_action( 'admin_menu', 'bpdev_groups_extra_setup_globals', 1 );

	add_action( 'admin_menu', 'bpdev_groups_extra_check_installed' );

	add_action( 'groups_group_details_edited', 'bpdev_groups_extra_add_field_requests' );
	add_action( 'groups_create_group_step1_save', 'bpdev_groups_extra_add_field_requests' );
	add_action( 'groups_custom_group_fields_editable', 'bpdev_groups_extra_add_field_edit_cicle' );
	add_action( 'groups_custom_group_fields', 'bpdev_groups_extra_add_field_home_cicle', 1 );

	if ( get_option( 'bpdev-group_extra-boxtitle' ) )
		add_filter( 'groups_extra_fields_boxtitle', 'bpdev_group_extra_first_boxtitle' );

}

function bpdev_groups_extra_setup_globals() {

	global $bp;

	$bp->bpdev->groups->extra->slug = 'group_extra';
	$bp->bpdev->groups->extra->table_name = "wp_{$bp->bpdev->prefix->tables}{$bp->bpdev->groups->extra->slug}_meta";

}

function bpdev_groups_extra_install() {

	global $wpdb, $bp;
	
	if ( !empty( $wpdb->charset ) )
		$charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}";
		
	$sql[] = "CREATE TABLE {$bp->bpdev->groups->extra->table_name} (
			id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
			group_id bigint(20) NOT NULL,
			meta_key varchar(255) DEFAULT NULL,
			meta_value longtext DEFAULT NULL,
			KEY group_id (group_id),
			KEY meta_key (meta_key)
		   ) {$charset_collate};";

	require_once( ABSPATH . 'wp-admin/upgrade-functions.php' );

	dbDelta( $sql );
	
	update_site_option( 'bpdev-groups_extra-db-version', BPDEV_GROUPS_EXTRA_DB_VERSION );

}

function bpdev_groups_extra_check_installed() {

	global $wpdb, $bp;

	if ( is_site_admin() ) {

		if ( ( !$wpdb->get_var( "SHOW TABLES LIKE '{$bp->bpdev->groups->extra->table_name}'" ) ) || ( get_site_option( 'bpdev-groups_extra-db-version' ) < BPDEV_GROUPS_EXTRA_DB_VERSION )  )
			bpdev_groups_extra_install();
	}

}

function bpdev_group_extra_admin_menu_save() {

	do_action( 'bpdev_admin_save_component' , 'bpdev-group_extra-status' );
	do_action( 'bpdev_admin_save_component' , 'bpdev-group_extra-boxtitle' );
	do_action( 'bpdev_theme_admin_menu_extra_save' );

}

function bpdev_group_extra_admin_menu_screen() {

	?>

	<h3><?php echo BPDEV_GROUPS_EXTRA_NAME; ?></h3>
	<table class="form-table">

		<tr valign="top">

			<th scope="row">Plugin Version</th>

			<td>
				<?php echo BPDEV_GROUPS_EXTRA_VERSION; ?>
			</td>

		</tr>
		<tr>

			<th scope="row">Donators (more than 100USD):</th>

			<td>
				<a href="http://spotpill.com">Pier Paolo Zomparelli</a>
			</td>

		</tr>
		<tr>

			<th scope="row">Plugin Status</th>

			<td>

				<select id="bpdev-group_extra-status" name="bpdev-group_extra-status">

					<option value="off"  <?php if ( !get_option( 'bpdev-group_extra-status' ) || 'off' == get_option( 'bpdev-group_extra-status' ) ) echo 'selected=""'; ?>>Off</option>

					<option value="on" <?php if( 'on' == get_option( 'bpdev-group_extra-status' ) ) echo 'selected=""'; ?>>On</option>

				</select>

			</td>

		</tr>

		<tr valign="top">

			<th scope="row">Box Title</th>
			<td>
				<input type="text" value="<?php echo get_option( 'bpdev-group_extra-boxtitle' ); ?>" id="bpdev-group_extra-boxtitle" name="bpdev-group_extra-boxtitle"/><br/>
					The title of the tables of extra fields (Example: About, Group Info)
			</td>

		</tr>

<?php do_action( 'bpdev_group_extra_admin_menu_extra_screen' ); ?>


	</table>

	<?php

}

function bpdev_groups_extra_register_field( $slug , $name , $type , $extra , $default = 'off' , $admin_show = 'on' , $condition = true , $priority = 1 ) {

	global $group_extras;

	if ( $condition ) {

		$group_extras->{$slug}->slug = $slug;
		$group_extras->{$slug}->name = $name;
		$group_extras->{$slug}->type = $type;
		$group_extras->{$slug}->extra = $extra;
		$group_extras->{$slug}->default = $default;
		$group_extras->{$slug}->priority = $priority + 2;
		$group_extras->{$slug}->admin = $admin_show;

	}

}



function bpdev_groups_extra_fields_setup_globals() {

	global $group_extras;

	do_action( 'bpdev_groups_extra_fields_setup_globals' );

}

function bpdev_groups_extra_add_field_requests( $group_id ) {

	global $group_extras, $group_obj;

	foreach ( $group_extras as $group_extra )
		bpdev_groups_extra_add_field_request( $group_extra->slug , $group_extra->name , $group_extra->type , $group_obj->id, $group_extra->extra );

}

function bpdev_groups_extra_add_field_request( $name, $title, $type = "multitext", $group_id,  $extra = false ) {

	if( 'checkbox' == $type ) {

		foreach ( $extra as $extra_check ) {

			$check_name = $name . '-' . $extra_check[0];

			if ( isset( $_POST['groups-'. $check_name] ) ) {

				bpdev_groups_extra_updatemeta( $group_id, 'groups_bpdev_' . $check_name, 1);

			} else {

				bpdev_groups_extra_updatemeta( $group_id, 'groups_bpdev_' . $check_name, "none");

			}

		}

	}

	if ( 'checkbox' != $type ) {

		if ( $_POST['groups-' . $name] ) {

			$post_value = $_POST['groups-' . $name];
			bpdev_groups_extra_updatemeta( $group_id, 'groups_bpdev_' . $name, $post_value );

		}

	}

}

function bpdev_groups_extra_add_field_edit( $name, $title, $type = "multitext", $group_id, $extra = false ) {

	switch ( $type ) {

		case "multitext":
			?>

			<label for="groups-<?php echo $name ?>"><?php _e( $title, 'buddypress' ); ?></label>
			<textarea name="groups-<?php echo $name ?>" id="groups-<?php echo $name ?>"><?php echo bpdev_groups_extra_getmeta( $group_id, 'groups_bpdev_' . $name); ?></textarea>

			<?php
		break;
		case "singletext":
			?>

			<label for="groups-<?php echo $name ?>"><?php _e( $title, 'buddypress' ); ?></label>
			<input name="groups-<?php echo $name ?>" id="groups-<?php echo $name ?>" type="text" value="<?php echo bpdev_groups_extra_getmeta( $group_id, 'groups_bpdev_' . $name); ?>">

			<?php
		break;
		case "checkbox":
			?>

			<label for="groups-<?php echo $name ?>"><?php _e( $title, 'buddypress' ); ?></label>

			<?php

			if ( is_array( $extra ) ) { 
			
				foreach ( $extra as $extra_check ) { 
				?>

			<label for="groups-<?php echo $name . '-' . $extra_check[0] ?>">
				<input name="groups-<?php echo $name . '-' . $extra_check[0] ?>" id="groups-<?php echo $name . '-' . $extra_check[0] ?>" type="checkbox" value="ok" <?php if(bpdev_groups_extra_getmeta( $group_id, 'groups_bpdev_' . $name . '-' . $extra_check[0]) == 1) echo "CHECKED"; ?>>
				<?php echo $extra_check[1]; ?>
			</label>

				<?php
				}
			} else {

			}

		break;
		case "dropdown":
?>
			<label for="groups-<?php echo $name ?>"><?php _e( $title, 'buddypress' ); ?></label>
			<?php if ( is_array( $extra ) ) {  ?>
				<select name="groups-<?php echo $name ?>">
				<?php foreach ( $extra as $extra_check ) : ?>
					<?php if ( !isset($extra_check[1]) ) $extra_check[1] = $extra_check[0]; ?>
					<option value="<?php echo $extra_check[1]; ?>" <?php if(bpdev_groups_extra_getmeta($group_id, 'groups_bpdev_' . $name) == $extra_check[1]) echo "SELECTED"; ?>><?php echo $extra_check[1]; ?></option>
				<?php endforeach; ?>
				</select>
				<?php
			} else {
			} ?>
<?php
		break;
	}
}
function bpdev_groups_extra_add_field_home( $name, $title, $type = "multitext", $group_id, $extra = false) {

	switch ($type) {

		case "multitext":
			if ( bpdev_groups_extra_getmeta( $group_id, 'groups_bpdev_' . $name ) ) {
			?>

			<tr id="groups-<?php echo $name; ?>">
				<td class="label"><?php echo apply_filters( 'groups_extra_fields_title', apply_filters( 'groups_extra_fields_title_' . $name , $title ) ); ?></td>
				<td class="data"><p><?php echo apply_filters( 'group_extra_fields_content', apply_filters( 'groups_extra_fields_content_' . $name, bpdev_groups_extra_getmeta( $group_id, 'groups_bpdev_' . $name ) ) ); ?></p></td>
			</tr>

			<?php
			}
			break;
		case "singletext":
			if ( bpdev_groups_extra_getmeta( $group_id, 'groups_bpdev_' . $name ) ) {
				?>

			<tr id="groups-<?php echo $name; ?>">
				<td class="label"><?php echo apply_filters( 'groups_extra_fields_title', apply_filters( 'groups_extra_fields_title_' . $name , $title ) ); ?></td>
				<td class="data"><p><?php echo apply_filters( 'group_extra_fields_content', apply_filters( 'groups_extra_fields_content_' . $name, bpdev_groups_extra_getmeta( $group_id, 'groups_bpdev_' . $name ) ) ); ?></p></td>
			</tr>

				<?php
			}
			break;
		case "checkbox":
			?>

			<tr id="groups-<?php echo $name; ?>">
				<td class="label"><?php echo apply_filters( 'groups_extra_fields_title', apply_filters( 'groups_extra_fields_title_' . $name , $title ) ); ?></td>
				<td class="data">
					<p>

			<?php

			if ( !isset( $extra ) ) {
				echo apply_filters( 'groups_extra_fields_no_value', "Not Definied Yet" );
			} else {
				$count = 0;
				foreach ( $extra as $is_checked ) {
					if ( bpdev_groups_extra_getmeta( $group_id, 'groups_bpdev_' . $name . '-' . $is_checked[0] ) == 1 ) {
						$extra_checks[$count] = array(
									$is_checked[0],
									$is_checked[1]
									);
						$count = $count + 1;
					}
				}
				if ( isset( $extra_checks ) ) {
					foreach ( $extra_checks as $extra_check ) : 
						if ( bpdev_groups_extra_getmeta($group_id, 'groups_bpdev_' . $name . '-' . $extra_check[0]) == 1 ) {
							if( $extra_checks[0][0] != $extra_check[0] ) echo ', ';
							echo apply_filters( 'group_extra_fields_content', apply_filters( 'groups_extra_fields_content_' . $name, $extra_check[1] ) );
						}
					endforeach;
				} else {
					echo apply_filters( 'groups_extra_fields_no_value', "Not Definied Yet" );
				}
			}

			?>

					</p>
				</td>
			</tr>
			<?php
		break;
		case "dropdown":
			if ( bpdev_groups_extra_getmeta( $group_id, 'groups_bpdev_' . $name ) ) {
			?>
			<tr id="groups-<?php echo $name; ?>">
				<td class="label"><?php echo apply_filters( 'groups_extra_fields_title', apply_filters( 'groups_extra_fields_title_' . $name , $title ) ); ?></td>
				<td class="data"><p><?php echo apply_filters( 'group_extra_fields_content', apply_filters( 'groups_extra_fields_content_' . $name, bpdev_groups_extra_getmeta( $group_id, 'groups_bpdev_' . $name ) ) ); ?></p></td>
			</tr>
			<?php
			}
		break;
	}
}
function bpdev_groups_extra_add_field_edit_cicle() {

	global $group_extras, $group_obj;

	foreach ($group_extras as $group_extra) 
		bpdev_groups_extra_add_field_edit($group_extra->slug, $group_extra->name, $group_extra->type, $group_obj->id, $group_extra->extra );

}
function bpdev_groups_extra_add_field_home_cicle() {

	global $group_extras, $group_obj;

	?>

<div class="info-group">

	<h4><?php echo apply_filters( 'groups_extra_fields_boxtitle', "About" );?></h4>

	<table class="profile-fields">
		<tbody>

	<?php

	foreach ( $group_extras as $group_extra )
		bpdev_groups_extra_add_field_home($group_extra->slug, $group_extra->name, $group_extra->type, $group_obj->id);

	?>

		</tbody>
	</table>

</div>

	<?php

}

function  bpdev_groups_extra_updatemeta( $group_id, $meta_key, $meta_value ) {

	global $bp;

	return  bpdev_framework_updatemeta( $group_id , $meta_key ,$meta_value , 'group_id' , $bp->bpdev->groups->extra->table_name );

}

function  bpdev_groups_extra_deletemeta( $group_id, $meta_key, $meta_value = false ) {

	global $bp;

	return bpdev_framework_deletemeta( $group_id ,  $meta_key , $meta_value , 'group_id' , $bp->bpdev->groups->extra->table_name );

}

function bpdev_groups_extra_getmeta( $group_id, $meta_key, $meta_value = false, $type = true ) {

	global $bp;

	if ( $type )
		return bpdev_framework_getmeta( $group_id , $meta_key , $meta_value , 'group_id' , $bp->bpdev->groups->extra->table_name );
	else
		return bpdev_framework_getmeta( false , $meta_key , $meta_value , 'meta_value' , $bp->bpdev->groups->extra->table_name , 'group_id' );

}

function bpdev_group_extra_search_by_name( $name ) {

	global $group_extras;

	$count = 0;

	foreach ( $group_extras as $group_extra ) {

		if ( $group_extra->slug == $name ) {

			$group_extras_id = $count;
		}

		$count = $count + 1;
	}

	return $group_extras_id;

}

function bpdev_group_extra_get_values_by_id( $id ) {

	global $group_extras;

	if ( isset( $group_extras->{$id}->extra ) )
		$group_extra_values = $group_extras->{$id}->extra;

	return $group_extra_values;

}

add_filter( 'groups_extra_fields_content_link', 'make_clickable' );
add_filter( 'groups_extra_fields_content', 'convert_chars' );
add_filter( 'groups_extra_fields_content', 'wpautop' );
add_filter( 'groups_extra_fields_content', 'strip_tags', 1 );

function bpdev_group_extra_first_boxtitle() {

	return get_option( 'bpdev-group_extra-boxtitle' );

}

/*add_filter( 'groups_extra_fields_content_the-type', 'xprofile_filter_link_profile_data', 1, 3 );*/
?>
